<!DOCTYPE HTML>
<html class="butler">
<head>
    <title>科寓共享家</title>
    <% include common/meta.ejs %>
    <% include common/public.ejs %>
    <%
       let stateMap = {
           '0': '未审核',
           '1': '执行中',
           '2': '已结束',
           '3': '已终止',
           '4': '已归档',
       };
    %>
</head>
<body>

<div class="weui-row weui-no-gutter contractTitle ">
    <div style="height: 50px;width: 20%">
        <a href="butlerIndex?openId=<%data.openId%>" style="color: #FFFFFF"><img src="<%= global.config.publicPath %>/page/images/back.png" ></a>
        <span > <a href="butlerIndex?openId=<%data.openId%>" style="color: #FFFFFF">返回</a></span>
    </div>
    <div  style="height: 50px;width: 60%">
    </div>
    <div  style="height: 50px;width: 20%">

    </div>
</div>
<div class="weui_search_bar weui_search_focusing" id="search_bar">
    <form class="weui_search_outer seachForm">
        <div class="weui_search_inner">
            <i class="weui_icon_search"></i>
            <input type="search" name="name_like" class="weui_search_input" id="search_input" placeholder="请输入房屋名称"/>
        </div>
        <label for="search_input" class="weui_search_text" id="search_text">
            <i class="weui_icon_search"></i>
            <span>搜索</span>
        </label>
    </form>
    <a href="javascript:" class="weui_search_cancel" id="search_cancel">搜索</a>
</div>
<div id="doing"></div>
<div class="weui-infinite-scroll">
    <div class="infinite-preloader"></div>
    正在加载...
</div>

<% include common/footer.ejs %>
<script>
    let loadFlag = false;
    let pageNumber = 0;
    let statusData = {
        properties: {}
    };
    function loadingData() {
        statusData.properties.pageSize = 7;
        statusData.properties.pageNumber = 0;
        if (!loadFlag) {
            loadFlag = true;
            Dolphin.ajax({
                url: CTX + '/api/509cb2a903d2466b9645432e025609c4',
                type: 'post',
                data: Dolphin.json2string(statusData),
                forceUrl: 'true',
                onSuccess: function (data) {
                    pageNumber++;
                    loadFlag = false;
                    let list = data.rows;
                    let contractId, con = '', con2 = '';

                    for (let i = 0; i < list.length; i++) {
                        con = con + `
                            <!--<div class="circles"></div>-->
                            <div class="block">
                            <h3>
                                <span class="stateIcon"></span>
                                ${list[i].houseName }
                            </h3>
                            <p>
                                <span class="label">房号</span>
                                <span>${list[i].house.houseNumber }</span>
                            </p>
                            <p>
                                <span class="label">状态</span>
                                <span>${list[i].statusName}</span>
                            </p>
                             <a href="javascript:changeContractStatus('${list[i].id}');"><span class="enterIcon"></span></a>
                            </div>
                        `

                    }
                    $('#doing').html(con);
                }
            })
        }
    };
    function reloadingData() {
        statusData.properties.pageSize = 7;
        statusData.properties.pageNumber = pageNumber;
        if (!loadFlag) {
            loadFlag = true;
            Dolphin.ajax({
                url: CTX + '/api/509cb2a903d2466b9645432e025609c4',
                type: 'post',
                data: Dolphin.json2string(statusData),
                forceUrl: 'true',
                onSuccess: function (data) {
                    pageNumber++;
                    loadFlag = false;
                    let list = data.rows;
                    let contractId, con = '', con2 = '';

                    for (let i = 0; i < list.length; i++) {
                        con = con + `
                            <!--<div class="circles"></div>-->
                            <div class="block">
                            <h3>
                                <span class="stateIcon"></span>
                                ${list[i].houseName }
                            </h3>
                            <p>
                                <span class="label">房号</span>
                                <span>${list[i].house.houseNumber }</span>
                            </p>
                            <p>
                                <span class="label">状态</span>
                                <span>${list[i].statusName}</span>
                            </p>
                             <a href="javascript:changeContractStatus('${list[i].id}');"><span class="enterIcon"></span></a>
                            </div>
                        `
                    }
                    $('#doing').append(con);
                }
            })
        }
    };
    //搜索框
    $('#search_cancel').click(function () {
        let data = Dolphin.form.getValue('.seachForm');
        let sendDatas={
            properties:{
                house_name_obj_like_ae:data.name_like
            }
        };
        Dolphin.ajax({
            url: CTX + '/api/509cb2a903d2466b9645432e025609c4',
            type: 'post',
            data: Dolphin.json2string(sendDatas),
            forceUrl: 'true',
            onSuccess: function (data) {
                let getAll='';
                let list=data.rows;
                if(list.length<=0){
                    getAll='<center>没有找到任何内容</center>';
                }else{
                    for(let i=0;i<list.length;i++){
                        getAll = getAll+`
                            <!--<div class="circles"></div>-->
                            <div class="block">
                            <h3>
                                <span class="stateIcon"></span>
                                ${list[i].houseName }
                            </h3>
                            <p>
                                <span class="label">房号</span>
                                <span>${list[i].house.houseNumber }</span>
                            </p>
                            <p>
                                <span class="label">状态</span>
                                <span>${list[i].statusName}</span>
                            </p>
                             <a href="javascript:changeContractStatus('${list[i].id}');"><span class="enterIcon"></span></a>
                            </div>
                        `
                    }
                }
                $('#doing').html(getAll);
            }
        });
    });
    $(document).ready(
        function () {
            loadingData();
        }
    );
    $(document.body).pullToRefresh();

    $(document.body).on("pull-to-refresh", function () {
        //do something
        loadingData();
        $(document.body).infinite(50);
    });

    $('.weui-infinite-scroll').css('display', 'none');

    $(document.body).infinite(20);

    var loading = false;  //状态标记
    $(document.body).infinite().on("infinite", function () {
        $('.weui-infinite-scroll').css('display', 'block');
        if (loading) return;
        loading = true;
        setTimeout(function () {
            if (!loadFlag) {
                reloadingData();
            }
            loading = false;

        }, 1500);   //模拟延迟
    });

    function changeContractStatus(contractId){
        $.actions({
            title: '更改合同状态',
            actions: (function () {
                let items = [];
                <% for(let key in stateMap){ %>
                items.push({
                    text: '<%= stateMap[key] %>',
                    onClick: function () {

                        $.confirm("您确定更改合同状态吗？", function() {
                            var obj = { properties : {
                                    id : contractId,
                                    status : `<%= key %>`
                                }
                            };
                            Dolphin.ajax({
                                url: CTX + '/api/26afdf2ad67044e58affbb49d08c4aaf',
                                type: 'put',
                                data: Dolphin.json2string(obj),
                                forceUrl: 'true',
                                onSuccess: function (data) {
                                    location.href = CTX + '/view/contractManager';
                                }
                            });
                        }, function() {
                            //点击取消后的回调函数
                        });
                    }
                })
                <% } %>
                return items;
            })()
        });
    }


</script>

</body>
</html>